package com.example.octopus.utils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DataBaseUtils {
    public static <T>String insert(String collection, List<T> data){
        Gson gson=new Gson();
        String s= "db.collection(\""+collection+"\").add({data:"+gson.toJson(data)+"})";
        return s;
    }
    public static  String delete(String collectionName, Map<String, Object> conditions) {
        Gson gson =new Gson();
        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("db.collection(\"").append(collectionName).append("\")");

        if (conditions != null && !conditions.isEmpty()) {
            queryBuilder.append(".where(").append(gson.toJson(conditions)).append(")");
        }

        queryBuilder.append(".remove()");

        return queryBuilder.toString();
    }

    public static  String select(String collectionName, Map<String, Object> conditions,int limit, int offset){
        Gson gson =new Gson();
        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("db.collection(\"").append(collectionName).append("\")");

        if (conditions != null && !conditions.isEmpty()) {
            queryBuilder.append(".where(").append(gson.toJson(conditions)).append(")");
        }
        if (limit > 0) {
            queryBuilder.append(".limit(").append(limit).append(")");
        }

        if (offset > 0) {
            queryBuilder.append(".skip(").append(offset).append(")");
        }

        queryBuilder.append(".get()");

        return queryBuilder.toString();
    }
    public static  String select(String collectionName, Map<String, Object> conditions,int offset){
        Gson gson =new Gson();
        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("db.collection(\"").append(collectionName).append("\")");

        if (conditions != null && !conditions.isEmpty()) {
            queryBuilder.append(".where(").append(gson.toJson(conditions)).append(")");
        }
        if (offset > 0) {
            queryBuilder.append(".skip(").append(offset).append(")");
        }
        queryBuilder.append(".get()");

        return queryBuilder.toString();
    }
    public static  String select(String collectionName,int offset){
        Gson gson =new Gson();
        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("db.collection(\"").append(collectionName).append("\")");

        if (offset > 0) {
            queryBuilder.append(".skip(").append(offset).append(")");
        }
        queryBuilder.append(".get()");
        return queryBuilder.toString();
    }
    public static  String select(String collectionName, Map<String, Object> conditions){
        Gson gson =new Gson();
        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("db.collection(\"").append(collectionName).append("\")");
        if (conditions != null && !conditions.isEmpty()) {
            queryBuilder.append(".where(").append(gson.toJson(conditions)).append(")");
        }
        queryBuilder.append(".get()");
        return queryBuilder.toString();
    }
    public static  String select(String collectionName){
        Gson gson =new Gson();
        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("db.collection(\"").append(collectionName).append("\")");
        queryBuilder.append(".limit(").append(20).append(")");
        queryBuilder.append(".get()");

        return queryBuilder.toString();
    }

    public static String update(String collectionName, String docId,Object data){
        Gson gson =new Gson();
        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("db.collection(\"").append(collectionName).append("\")");
        if (docId != null && !docId.isEmpty()) {
            queryBuilder.append(".doc(\"").append(docId).append("\")");
        }
        queryBuilder.append(".update({data:").append(gson.toJson(data)).append("})");
        return queryBuilder.toString();
    }
    public static  String select(String collectionName, String conditions){
        Gson gson =new Gson();
        StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("db.collection(\"").append(collectionName).append("\")");

        if (conditions != null && !conditions.isEmpty()) {
            queryBuilder.append(".where(").append(conditions).append(")");
        }
        queryBuilder.append(".limit(").append(10).append(")");
        queryBuilder.append(".get()");
        return queryBuilder.toString();
    }
}
